<template>
  <el-card shadow="never" style="border: none">
    <div slot="header">
      <i class="el-icon-ticket" />
      <span>巡查内容</span>
    </div>

    <el-table v-loading="loading" :data="list" :span-method="merge">
      <el-table-column label="核查指标" prop="index" />
      <el-table-column label="核查要点" prop="key" />
      <el-table-column label="核查内容" prop="content" />
      <el-table-column label="核查结果" width="200">
        <template slot-scope="scope">
          <span>{{ scope.row.result }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="附件" prop="files" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:task:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="修改核查内容" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="核查指标" prop="index">
          <el-input v-model="form.index" disabled />
        </el-form-item>
        <el-form-item label="核查要点" prop="key">
          <el-input v-model="form.key" disabled />
        </el-form-item>
        <el-form-item label="核查内容" prop="content">
          <el-input v-model="form.content" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
import { listRole, updateRole } from '@/api/system/role'

export default {
  name: 'SecurityCheckList',
  props: {
    taskNo: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      loading: true,
      list: [],
      rowSpanArr: [],
      // 是否显示弹出层
      open: false,
      form: {},
      rules: {
        content: [
          { required: true, message: '核查内容不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.prevRow = null
      this.mergeRow = 1
      listRole().then(response => {
        // this.list = response.rows
        this.list = [
          { index: '123', key: 'key1', content: 'xxx' },
          { index: '123', key: 'key2', content: 'xxx' },
          { index: 'abc', key: 'key3', content: 'xxx' },
          { index: 'abc', key: 'key4', content: 'xxx' },
          { index: 'abc', key: 'key5', content: 'xxx' },
          { index: '123abc', key: 'key6', content: 'xxx' },
          { index: 'abc123', key: 'key7', content: 'xxx' }
        ]
        this.loading = false
        this.getRowSpan()
      })
    },
    getRowSpan() {
      let rowFlag = 0
      for (let i = 0, len = this.list.length; i < len; i++) {
        if (i === 0) {
          this.rowSpanArr.push(1)
        } else {
          if (this.list[i].index === this.list[i - 1].index) {
            this.rowSpanArr[rowFlag] += 1
            this.rowSpanArr.push(0)
          } else {
            this.rowSpanArr.push(1)
            rowFlag = i
          }
        }
      }
    },
    // 合并表格行
    merge({ rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        return {
          rowspan: this.rowSpanArr[rowIndex],
          colspan: this.rowSpanArr[rowIndex] ? 1 : 0
        }
      }
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {}
      this.resetForm('form')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = { ...row }
      this.open = true
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateRole(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
